package net.jansensan.as3maxlikeui
{
	// + ----------------------------------------
	//		[ IMPORTS ]
	// + ----------------------------------------
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	
	/**
	 * <p>Write short class description here</p>
	 *
	 * @author Mat Janson Blanchet
	 */
	public class OutputBox extends Sprite implements IMaxLikeResizableObject
	{
		// + ----------------------------------------
		//		[ CONSTANTS ]
		// + ----------------------------------------
		
		private	const	MIN_SIZE		:uint = 24;
		private	const	RADIUS			:uint = 8;
		private	const	LINE_THICKNESS	:uint = 2;
		private	const	LINE_COLOR		:uint = 0x6d6d6d;
		private	const	BODY_COLOR		:uint = 0xffffff;
		
		
		// + ----------------------------------------
		//		[ VARIABLES ]
		// + ----------------------------------------
		
		private	var	_bg		:Sprite;
		private	var	_body	:Sprite;
		private	var	_tf		:TextField;
		
		private	var	_width	:int;
		private	var	_height	:int;
		private	var	_text	:String;
		
		
		// + ----------------------------------------
		//		[CONSTRUCTOR ]
		// + ----------------------------------------
		
		public function OutputBox	(	text:String = "",
										initWidth:uint = 80,
										initHeight:uint = MIN_SIZE
									)
		{
			super();
			_text = text;
			_width = initWidth;
			_height = initHeight;
		}
		
		
		// + ----------------------------------------
		//		[ PUBLIC METHODS ]
		// + ----------------------------------------
		
		public function init():void
		{
			_bg = new Sprite();
			
			_body = new Sprite();
			
			var f:TextFormat = new TextFormat();
			f.font = "Arial";
			f.size = 16;
			
			_tf = new TextField();
			_tf.defaultTextFormat = f;
			_tf.multiline = true;
			_tf.wordWrap = true;
			
			resize(_width, _height);
			setText();
			
			addChild(_bg);
			addChild(_body);
			addChild(_tf);
		}
		
		
		public function resize(newWidth:int, newHeight:int):void
		{
			_width = newWidth;
			_height = newHeight;
			
			if(_bg)
			{
				_bg.graphics.clear();
				_bg.graphics.beginFill(LINE_COLOR);
				_bg.graphics.drawRoundRect(0, 0, _width, _height, RADIUS, RADIUS);
				_bg.graphics.endFill();
			}
			
			if(_body)
			{
				_body.graphics.clear();
				_body.graphics.beginFill(BODY_COLOR);
				_body.graphics.drawRoundRect(LINE_THICKNESS, LINE_THICKNESS, _width - LINE_THICKNESS * 2, _height - LINE_THICKNESS * 2, RADIUS - LINE_THICKNESS, RADIUS - LINE_THICKNESS);
				_body.graphics.endFill();
			}
			
			if(_tf)
			{
				_tf.width = _bg.width - RADIUS * 2;
				_tf.height = _bg.height - RADIUS * 2;
				_tf.x = RADIUS;
				_tf.y = RADIUS;
			}
		}
		
		
		public function show():void
		{
			visible = true;
		}
		
		
		public function hide():void
		{
			visible = false;
		}
		
		
		public function kill():void
		{
			
		}
		
		
		// + ----------------------------------------
		//		[ PRIVATE/PROTECTED METHODS ]
		// + ----------------------------------------
		
		private function setText():void
		{
			if(_tf) _tf.htmlText = _text;
		}
		
		
		// + ----------------------------------------
		//		[ EVENT HANDLERS ]
		// + ----------------------------------------
		
		
		
		
		// + ----------------------------------------
		//		[ GETTERS / SETTERS ]
		// + ----------------------------------------
		
		override public function get width():Number
		{
			return _width;
		}
		
		
		override public function get height():Number
		{
			return _height;
		}
		
		
		public function set text(value:String):void
		{
			_text = value;
			setText();
		}
		
		
	}
}